package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditProcessPlanAdapter;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditUnusualCyclePlanAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartDailyFeedbackBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartFeedbackResponse;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.produce.bean.request.WorkPartScheduleItemRequest;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderWorkPartScheduleEditView;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IWorkPartDetailView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderWorkPartScheduleEditPresenter;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.WorkPartDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ProduceModifyPlanActivity extends BaseActivity implements IWorkPartDetailView, IProductOrderWorkPartScheduleEditView {
    private static String TAG = ProduceModifyPlanActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_cycle_type;
    private ListViewForScrollView lv_cycle, lv_abnormal_cycle;
    private LinearLayout ll_abnormal_cycle;
    private Button btn_upload;

    private WorkPartBean workPartBean;
    private WorkPartFeedbackResponse workPartFeedbackResponse;
    private WorkPartDailyFeedbackBean workPartDailyFeedbackBean;

    private ProduceEditProcessPlanAdapter naoamlAdapter;
    private ProduceEditUnusualCyclePlanAdapter unusualAdapter;

    private WorkPartDetailPresenter workPartDetailPresenter;
    private ProductOrderWorkPartScheduleEditPresenter productOrderWorkPartScheduleEditPresenter;
    private WorkPartScheduleItemRequest workPartScheduleItemRequest = new WorkPartScheduleItemRequest();

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_modify_plan);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_cycle_type = findViewById(R.id.tv_cycle_type);
        lv_cycle = findViewById(R.id.lv_cycle);
        lv_abnormal_cycle = findViewById(R.id.lv_abnormal_cycle);
        ll_abnormal_cycle = findViewById(R.id.ll_abnormal_cycle);
        btn_upload = findViewById(R.id.btn_upload);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.modify_produce_plan));

        Intent intent = getIntent();
        workPartFeedbackResponse = (WorkPartFeedbackResponse) intent.getSerializableExtra(ConstantUtil.KEY_1);
        workPartDailyFeedbackBean = (WorkPartDailyFeedbackBean) intent.getSerializableExtra(ConstantUtil.KEY_2);

        naoamlAdapter = new ProduceEditProcessPlanAdapter(this);
        unusualAdapter = new ProduceEditUnusualCyclePlanAdapter(this);

        productOrderWorkPartScheduleEditPresenter = new ProductOrderWorkPartScheduleEditPresenter(this);

        workPartDetailPresenter = new WorkPartDetailPresenter(this);
        workPartDetailPresenter.workPartDetail();


        CommonEnum.WorkPartType type = workPartFeedbackResponse.getType();
        if (null != type) {
            if (type == CommonEnum.WorkPartType.APPLY_ADJUST) {
                ll_abnormal_cycle.setVisibility(View.VISIBLE);
                tv_cycle_type.setText(getResources().getString(R.string.adjust_cycle));

            } else if (type == CommonEnum.WorkPartType.APPLY_RETURN) {
                ll_abnormal_cycle.setVisibility(View.VISIBLE);
                tv_cycle_type.setText(getResources().getString(R.string.rework_cycle));

            } else if (type == CommonEnum.WorkPartType.APPLY_SCRAP) {
                ll_abnormal_cycle.setVisibility(View.VISIBLE);
                tv_cycle_type.setText(getResources().getString(R.string.tv_scrap_cycle));
            }
        }


    }


    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                Intent mIntent = new Intent();
                mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
                finish();
                break;
            // 上传
            case R.id.btn_upload:
                productOrderWorkPartScheduleEditPresenter.productOrderWorkPartScheduleEdit();
                break;
        }
    }

    @Override
    public String getOrderId() {
        return workPartFeedbackResponse.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartFeedbackResponse.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartFeedbackResponse.getWorkPartId();
    }

    @Override
    public List<WorkPartScheduleBean> getList() {
        return workPartBean.getWorkPartScheduleList();
    }

    @Override
    public void getSuccessed() {

    }

    @Override
    public void getFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartFeedbackResponse.getWorkPartOrderId();
    }

    @Override
    public void workPartDetailSueecssed(WorkPartBean workPartBean) {
        if (null != workPartBean) {
            this.workPartBean = workPartBean;
            naoamlAdapter.setData(workPartBean.getWorkPartScheduleList());
            lv_cycle.setAdapter(naoamlAdapter);

            CommonEnum.WorkPartType type = workPartFeedbackResponse.getType();
            if (null != type) {
//                if (type == CommonEnum.WorkPartType.UNUSUAL) {
//
//                    CommonEnum.WorkPartSubType subType = workPartDailyFeedbackBean.getSubType();
//                    if (null != subType) {
//                        if (subType == CommonEnum.WorkPartSubType.PRODUCE_UNUSUAL) {
//                            workPartScheduleItemRequest.getSpecifiedDayAfter(workPartBean.getCompleteDate(),workPartDailyFeedbackBean.getUnusualDays());
//                        }
//                    }
//                }
                if (type == CommonEnum.WorkPartType.APPLY_ADJUST) {
                    workPartScheduleItemRequest.getSpecifiedDayAfter(workPartBean.getCompleteDate(), workPartDailyFeedbackBean.getApplyDays());

                } else if (type == CommonEnum.WorkPartType.APPLY_RETURN) {
                    workPartScheduleItemRequest.getSpecifiedDayAfter(workPartBean.getCompleteDate(), workPartDailyFeedbackBean.getApplyDays());

                } else if (type == CommonEnum.WorkPartType.APPLY_SCRAP) {
                    workPartScheduleItemRequest.getSpecifiedDayAfter(workPartBean.getCompleteDate(), workPartDailyFeedbackBean.getApplyDays());
                }
            }

            unusualAdapter.setData(workPartScheduleItemRequest.getWorkPartScheduleItemRequestList());
            lv_abnormal_cycle.setAdapter(unusualAdapter);
        }
    }

    @Override
    public void workPartDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
